//
//  TZTestCell.m
//  TZImagePickerController
//
//  Created by 谭真 on 16/1/3.
//  Copyright © 2016年 谭真. All rights reserved.
//

#import "PhotoCollectCell.h"





@interface PhotoCollectCell()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation PhotoCollectCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _imageView = [[UIImageView alloc] init];
        _imageView.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.500];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.userInteractionEnabled = YES;
        _imageView.frame = self.bounds;
        [self addSubview:_imageView];
        self.clipsToBounds = YES;
        
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage imageNamed:@"chaCanmerDark"] forState:UIControlStateNormal];
        _deleteBtn.imageEdgeInsets = UIEdgeInsetsMake(-10, 0, 0, -10);
        _deleteBtn.alpha = 0.6;
        _deleteBtn.frame = CGRectMake(self.bounds.size.width - 36, 0, 36, 36);
        [_deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteBtn];
    
    }
    return self;
}

- (void)delete
{
    if ([_delegate respondsToSelector:@selector(PhotoCollectCellDeleteImage:pic:)]) {
        [_delegate PhotoCollectCellDeleteImage:self pic:self.pics];
    }
}


- (void)setIsAdd:(BOOL)isAdd
{
    _isAdd = isAdd;
    if (isAdd) {
        _deleteBtn.hidden = YES;
        _imageView.image = [UIImage imageNamed:@"tianjiaxiangce"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addImage)];
        [_imageView addGestureRecognizer:tap];
    }else {
        _deleteBtn.hidden = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage)];
        [_imageView addGestureRecognizer:tap];
    }
}

- (void)addImage
{
    if ([self.delegate respondsToSelector:@selector(PhotoCollectCellAddImage:)]) {
        [self.delegate PhotoCollectCellAddImage:self];
    }
}

- (void)showImage
{
    if ([_delegate respondsToSelector:@selector(PhotoCollectCellShowImage:index:)]) {
        [_delegate PhotoCollectCellShowImage:self index:self.indexPath.row];
    }
}

- (void)setPics:(Pics *)pics
{
    _pics = pics;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:pics.pic_url] placeholderImage:[UIImage imageNamed:@"AJMP_placeholder_picture"] options:SDWebImageRetryFailed];
}

@end
